﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YJKExcelFormTool.Analyser;
using YJKExcelFormTool.DataDefine;
using YJKExcelFormTool.Kits;
using YJKExcelFormTool.Utilities;

namespace YJKExcelFormTool.Forms
{
	public partial class ImportExcelTableHeadForm : System.Windows.Forms.Form
	{
		public TableHead SelectTableHead
		{
			get
			{
				return this.m_HeadList[this.m_SelectHeadIndex];
			}
		}

		public ImportExcelTableHeadForm(string filePath, List<string> properties, string fontName, double fontHeight, double fontWidthScale)
		{
			this.m_TemplateFilePath = filePath;
			this.m_ShowMapping = true;
			this.m_Properties = properties;
			this.InitializeComponent();
			this.m_ExcelTableAnalyser = new ExcelTableAnalyser();
			if (!this.m_ExcelTableAnalyser.Init(this.m_TemplateFilePath))
			{
				return;
			}
			List<string> list = new List<string>();
			this.m_ExcelTableAnalyser.GetSheetsWithDoc(this.m_TemplateFilePath, ref list);
			foreach (string item in list)
			{
				this.cmbSheets.Items.Add(item);
				this.m_HeadList.Add(new TableHead());
			}
			if (this.cmbSheets.Items.Count > 0)
			{
				this.cmbSheets.SelectedIndex = 0;
			}
			this.m_FontName = fontName;
			this.m_FontHeight = fontHeight;
			this.m_FontWidthScale = fontWidthScale;
			this.m_Previewer = new Previewer(this, this, this.panelPreview.Left, this.panelPreview.Top, this.panelPreview.Width, this.panelPreview.Height, 1u);
			base.MouseWheel += this.m_Previewer.MouseWheel;
		}

		public ImportExcelTableHeadForm(string filePath, string fontName, double fontHeight, double fontWidthScale)
		{
			this.m_TemplateFilePath = filePath;
			this.m_ShowMapping = false;
			this.InitializeComponent();
			this.m_ExcelTableAnalyser = new ExcelTableAnalyser();
			if (!this.m_ExcelTableAnalyser.Init(this.m_TemplateFilePath))
			{
				return;
			}
			List<string> list = new List<string>();
			this.m_ExcelTableAnalyser.GetSheetsWithDoc(this.m_TemplateFilePath, ref list);
			foreach (string item in list)
			{
				this.cmbSheets.Items.Add(item);
				this.m_HeadList.Add(new TableHead());
			}
			if (this.cmbSheets.Items.Count > 0)
			{
				this.cmbSheets.SelectedIndex = 0;
			}
			this.m_FontName = fontName;
			this.m_FontHeight = fontHeight;
			this.m_FontWidthScale = fontWidthScale;
			this.m_Previewer = new Previewer(this, this, this.panelPreview.Left, this.panelPreview.Top, this.panelPreview.Width, this.panelPreview.Height, 1u);
			base.MouseWheel += this.m_Previewer.MouseWheel;
		}

		protected override void DefWndProc(ref Message m)
		{
			int msg = m.Msg;
			if (msg == 1125)
			{
				if (m.WParam.ToInt32() == 1)
				{
					if (this.m_HeadList[this.m_SelectHeadIndex].IsNull())
					{
						return;
					}
					TableDrawingKitForPicture tableDrawingKitForPicture = new TableDrawingKitForPicture(this.m_HeadList[this.m_SelectHeadIndex], this.m_FontHeight);
					try
					{
						tableDrawingKitForPicture.Drawing();
					}
					catch (Exception)
					{
					}
					this.m_Previewer.Curves = tableDrawingKitForPicture.Curves;
					this.m_Previewer.TextNotes = tableDrawingKitForPicture.Texts;
					this.m_Previewer.FontName = this.m_FontName;
					this.m_Previewer.FontHeight = Common.MMToFeet(this.m_FontHeight);
					return;
				}
			}
			else
			{
				base.DefWndProc(ref m);
			}
		}

		private void cmbSheets_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.m_SelectHeadIndex = this.cmbSheets.SelectedIndex;
			this.ChangeHeadInfos();
			this.ReadColumnMappingInfo(this.m_HeadList[this.m_SelectHeadIndex]);
			this.SetColMappint();
		}

		private void ImportExcelTableHeadForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (base.DialogResult == DialogResult.OK)
			{
				this.m_ExcelTableAnalyser.Close();
				if (this.m_ShowMapping)
				{
					TableHead tableHead = this.m_HeadList[this.m_SelectHeadIndex];
					HeadColumnMappingScheme headColumnMappingScheme = new HeadColumnMappingScheme(tableHead.TableSourceInfo.Name, tableHead.TableSourceInfo.SheetIndex);
					foreach (TableHeadCell tableHeadCell in tableHead.GetEffectiveRow())
					{
						headColumnMappingScheme.AddMappingItem(new KeyValuePair<string, string>(tableHeadCell.Value, tableHeadCell.Key.KeyName));
					}
					TableHeadColumnMappingStorage.WriteColumnMappingInfo(headColumnMappingScheme);
				}
			}
		}

		private void dbgPropertiesMapping_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			System.Windows.Forms.ComboBox comboBox = e.Control as System.Windows.Forms.ComboBox;
			if (comboBox != null)
			{
				comboBox.SelectedIndexChanged += this.DGVComboBoxSelectionChanged;
			}
		}

		private void DGVComboBoxSelectionChanged(object sender, EventArgs e)
		{
			int rowIndex = this.dbgPropertiesMapping.CurrentCell.RowIndex;
			string text = ((System.Windows.Forms.ComboBox)sender).Text;
			this.m_HeadList[this.m_SelectHeadIndex].GetEffectiveRow()[rowIndex].Key = new TableHeadCellKey(text);
		}

		private void ChangeHeadInfos()
		{
			if (this.m_HeadList[this.m_SelectHeadIndex].IsNull())
			{
				TableHead value = null;
				this.m_ExcelTableAnalyser.ReadDoc(this.m_TemplateFilePath, this.m_SelectHeadIndex, ref value);
				this.m_HeadList[this.m_SelectHeadIndex] = value;
			}
			this.Refresh();
		}

		private void SetColMappint()
		{
			if (!this.m_ShowMapping)
			{
				return;
			}
			this.dbgPropertiesMapping.Rows.Clear();
			List<string> list = new List<string>();
			foreach (string item in this.m_Properties)
			{
				list.Add(item);
			}
			list.Add("");
			List<TableHeadCell> effectiveRow = this.m_HeadList[this.m_SelectHeadIndex].GetEffectiveRow();
			int i = 0;
			int num = 1;
			while (i < effectiveRow.Count)
			{
				DataGridViewRow dataGridViewRow = new DataGridViewRow();
				DataGridViewTextBoxCell dataGridViewTextBoxCell = new DataGridViewTextBoxCell();
				dataGridViewTextBoxCell.Value = effectiveRow[i].Value;
				dataGridViewRow.Cells.Add(dataGridViewTextBoxCell);
				DataGridViewComboBoxCell dataGridViewComboBoxCell = new DataGridViewComboBoxCell();
				foreach (string item2 in list)
				{
					dataGridViewComboBoxCell.Items.Add(item2);
				}
				string keyName = this.m_HeadList[this.m_SelectHeadIndex].GetEffectiveRow()[i].Key.KeyName;
				int num2 = list.FindIndex((string s) => s.CompareTo(keyName) == 0);
				if (-1 != num2 && keyName.CompareTo("") != 0)
				{
					dataGridViewComboBoxCell.Value = keyName;
				}
				else
				{
					dataGridViewComboBoxCell.Value = list[num - 1];
					this.m_HeadList[this.m_SelectHeadIndex].GetEffectiveRow()[i].Key = new TableHeadCellKey(list[num - 1]);
				}
				dataGridViewRow.Cells.Add(dataGridViewComboBoxCell);
				this.dbgPropertiesMapping.Rows.Add(dataGridViewRow);
				num++;
				i++;
			}
			if (effectiveRow.Count > 8)
			{
				this.cMainSpaceCategory.Width = 100;
			}
		}

		private void ReadColumnMappingInfo(TableHead head)
		{
			if (!this.m_ShowMapping)
			{
				return;
			}
			HeadColumnMappingScheme headColumnMappingScheme = new HeadColumnMappingScheme(head.TableSourceInfo.Name, head.TableSourceInfo.SheetIndex);
			if (headColumnMappingScheme.SourceName.CompareTo("") == 0)
			{
				return;
			}
			if (TableHeadColumnMappingStorage.ReadColumnMappingInfo(ref headColumnMappingScheme))
			{
				List<TableHeadCell> effectiveRow = head.GetEffectiveRow();
				for (int i = 0; i < effectiveRow.Count; i++)
				{
					string name = head.GetEffectiveRow()[i].Value;
					int num = headColumnMappingScheme.MappingInfo.FindIndex((KeyValuePair<string, string> s) => s.Key.CompareTo(name) == 0);
					if (-1 != num)
					{
						head.GetEffectiveRow()[i].Key = new TableHeadCellKey(headColumnMappingScheme.MappingInfo[num].Value);
					}
				}
			}
		}

		private void ImportExcelTableHeadForm_Load(object sender, EventArgs e)
		{
			if (!this.m_ShowMapping)
			{
				this.btnOk.Left = 358;
				this.btnCancel.Left = 436;
				base.Width = 535;
				this.dbgPropertiesMapping.Visible = false;
				return;
			}
			this.ReadColumnMappingInfo(this.m_HeadList[this.m_SelectHeadIndex]);
			this.SetColMappint();
		}

		private ExcelTableAnalyser m_ExcelTableAnalyser;

		private List<TableHead> m_HeadList = new List<TableHead>();

		private string m_TemplateFilePath = "";

		private List<string> m_Properties = new List<string>();

		private int m_SelectHeadIndex;

		private string m_FontName = "";

		private double m_FontHeight;

		private double m_FontWidthScale = 1.0;

		private bool m_ShowMapping;

		private Previewer m_Previewer;
	}
}
